package leju.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
 * 页码控件
 * @author liuwei
 *
 */
public class PagePoint extends RadioGroup {
	// button显示的图片的资源id
	private int buttonDrawableResID = 0;

	private Context mContext;

	public PagePoint(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);

	}

	public PagePoint(Context context) {
		super(context);
		init(context);
	}

	void init(Context context) {
		mContext = context;
		setGravity(Gravity.CENTER);
		setOrientation(HORIZONTAL);
	setEnabled(false);
	
	  
		
		
	}

	//设置背景图片
	public void setBackground(int res){
		buttonDrawableResID = res;
	}
	
	// 设置显示的总页数
	public void setPagesTotle(int totle) {
		if (totle <= 0) {
			return;
		}
		
		int currentPageNum = getChildCount();
		int addPageNum = totle - currentPageNum;
		if (addPageNum > 0) {
			for (int i = 0; i < addPageNum; i++) {
				addRadioButton();
			}
		} else if (addPageNum < 0) {
			for (int i = 0; i > -addPageNum; i++) {
				addRadioButton();
				removeViewAt(totle);
			}
		}

	}

	/**
	 * 添加一个radiobutton
	 */
	private void addRadioButton() {
		RadioButton button = new RadioButton(mContext);
		if (buttonDrawableResID != 0) {
			//button.setButtonDrawable(buttonDrawableResID);
			button.setButtonDrawable(null);
			button.setBackgroundResource(buttonDrawableResID);
			button.setGravity(Gravity.CENTER);
			button.setText("");
			button.setClickable(false);
			

			

		}
		addView(button);
	}
   /**
    * 设置当前的选中的页数
    * @param checkId
    */
	public void setCheckPage(int checkId) {
		if (checkId < 0 || checkId > getChildCount()) {
			return;
		}
		RadioButton button=(RadioButton) getChildAt(checkId);
		button.setChecked(true);
	}


	 
}
